package com.misty.common.Util;

import cn.hutool.core.util.ObjectUtil;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : TimeUtil
 * @Description :
 * @Author :
 * @Date: 2023-01-03 14:03
 */
public class TimeUtil {

    public static Map getDisTime(LocalDateTime pastTime, LocalDateTime nowTime) {
        LocalDateTime now = ObjectUtil.isNull(nowTime)?LocalDateTime.now():nowTime;
        Map<String, Object> map = new HashMap<>();
        long disYear = ChronoUnit.YEARS.between(pastTime, now);
        if(disYear > 0){
            map.put("unit","年");
            map.put("time",disYear);
            return map;
        }
        long disMonth = ChronoUnit.MONTHS.between(pastTime, now);
        if(disMonth > 0){
            map.put("unit","月");
            map.put("time",disMonth);
            return map;
        }
        long disDay = ChronoUnit.DAYS.between(pastTime, now);
        if(disDay > 0){
            map.put("unit","日");
            map.put("time",disDay);
            return map;
        }
        long disHour = ChronoUnit.HOURS.between(pastTime, now);
        if(disHour > 0){
            map.put("unit","时");
            map.put("time",disHour);
            return map;
        }
        long disMinute = ChronoUnit.MINUTES.between(pastTime, now);
        if(disMinute > 0){
            map.put("unit","分");
            map.put("time",disMinute);
            return map;
        }
        long disSecond = ChronoUnit.SECONDS.between(pastTime, now);
        if(disSecond > 0){
            map.put("unit","秒");
            map.put("time",disSecond);
            return map;
        }
        return map;
    }
}
